#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Author  : tester
# @Site    : DONGFANG_HOTEL-10F
# @Software: PyCharm
# @datetime:2020/9/28 19:45
# @license: (C) Copyright 2018-2027, By 金锡科技.
"""


import pymysql
import yaml


class MysqlConnect(object):
    def __init__(self):
        self.f_yaml = open('../conf/mysql_config', 'r', encoding='utf-8')
        self.y = yaml.load(self.f_yaml, Loader=yaml.FullLoader)
        mysql_data = self.y['test1']
        mysql_host = mysql_data['host']
        mysql_post = mysql_data['port']
        mysql_user = mysql_data['user']
        mysql_passwd = mysql_data['passwd']
        mysql_db = mysql_data['travel_db']

        self.db_conn = pymysql.connect(host=mysql_host, port=mysql_post, user=mysql_user,
                                       passwd=mysql_passwd, db=mysql_db)
        self.cursor = self.db_conn.cursor()

    def sql_updata(self, updata_sql):
        sql = updata_sql
        try:
            self.cursor.execute(sql)
            self.db_conn.commit()
        except:
            print('Error: unable to update data')
            self.db_conn.rollback()

    def sql_select(self, select_sql):
        sql = select_sql
        select_data = ''
        try:
            self.cursor.execute(sql)
            results = self.cursor.fetchall()

            for row in results:
                select_data = row[0]
        except:
            print('Error: unable to fetch data')

        return select_data

    def delete_data(self):
        pass

    def close_mysql_conn(self):
        self.cursor.close()
        self.db_conn.close()
